package game.tileMap.view
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import game.tileMap.GameCamera;
	import game.tileMap.MapConst;
	import game.tileMap.SpriteCache;
	import game.tileMap.model.GameMapModel;
	import game.tileMap.model.TileMapModel;
	import game.tileMap.model.Troop;
	import game.tileMap.model.vo.Character;
	import game.tileMap.model.vo.MapObj;
	import game.tileMap.model.vo.Unit;
	import game.tileMap.view.TileMapCanvs;
	
	import mk.gamemap.model.Player;
	import mk.gamemap.view.sprite.CharView;
	import mk.gamemap.view.sprite.SpriteView;

	public class TileMapView extends EventDispatcher
	{
		
		public static const MOVE_END:String = "move_end";
		
		//public var mapModel:TileMapModel
		public var mapCanvas:TileMapCanvs;
		public var spCanvas:Sprite
		public var canvas:Sprite
		public var tileSize:int=MapConst.TileSize;
		public var player:CharView
		public var party:PartySet
		public var sprites:Array
		public var unitDict:Dictionary;
		private var spFactory:SpriteCache;
		
		
		
		private var mapModel:GameMapModel
		
		var scroll_rec:Rectangle
		var maxRec:Rectangle
		
		private var camera:GameCamera
		
		public function TileMapView(mapModel:GameMapModel)
		{
			this.mapModel=mapModel;
			spFactory=new SpriteCache();
			spCanvas=new Sprite();
			canvas=new Sprite();
			
			mapCanvas=new TileMapCanvs();
			mapCanvas.setData(mapModel.mapData.data);
			
			canvas.addChild(mapCanvas);
			canvas.addChild(spCanvas);
			sprites=[];
			unitDict=new Dictionary();
			
			scroll_rec=new Rectangle(0,0,400,400);//显示的范围
			canvas.scrollRect=scroll_rec;//确定地图的可显示范围
			maxRec=canvas.getBounds(canvas);//以自己的坐标系返回矩形
			
			camera=new GameCamera(0,0,400,400);
			camera.setBounds(0,0,500,4000);
			
		}
		
		public function drawMap():void{
			
			
			party=new PartySet(mapModel.troops[0]);
			addParty()
//			for each(var mapSp:MapObj in mapModel.objArr){
//				
//				
//				if (mapSp){
//					
//					
//					
//					spCanvas.addChild(player.sp);
//					trace("加入"+mapSp)
//					mapSprites.push(player);
//				}
//			
//				
//			}
			for (var i:int=1;i<mapModel.troops.length;i++){
				var troop:Troop=mapModel.troops[i]
					addTroop(troop);
			}
			
			var player:PlayerView=party.members[0]
			camera.follow(player.sp,1);
			
		}
		
		/**
		 *取得UnitView 
		 * @param unit
		 * @return 
		 * 
		 */		
		public function getUnit(unit:Unit):UnitView{
			return unitDict[unit];
		}
		
		
		public function addParty(){
			for each(var unit:PlayerView in party.members){
				if (unit){
					unit.addEventListener(UnitView.MOVE_END,moveEndHandler);
					spCanvas.addChild(unit.sp);
					unitDict[unit.vo]=unit;
				//	sprites.push(unit);
				}
			}
		}
		
		public function addTroop(troop:Troop){
			for each(var unit:Unit in troop.members){
				if (unit){
					var view:CharView=new UnitView(unit);
					view.addEventListener(UnitView.MOVE_END,moveEndHandler);
					spCanvas.addChild(view.sp);
					unitDict[unit]=view;
				//	sprites.push(view);
				}
				
				
			}
		}
		
		private function moveEndHandler(evt:Event):void{
			dispatchEvent(new Event(MOVE_END));
			
		}
		
		
		public function fight():void{
			trace("攻击开始")
		}
		
		
		private function clearSp(sp:Sprite):void{
			while(sp.numChildren>0){
				sp.removeChildAt(0);
			}
		}
		public function dispose():void{
			clearSp(spCanvas);
			clearSp(mapCanvas);
			sprites=[];
		}
		
		public function scroll_h(distance:int):void{
			scroll_rec.x-=distance;
			canvas.scrollRect=scroll_rec;
		}
		public function scroll_v(distance:int):void{
			scroll_rec.y-=distance;
			canvas.scrollRect=scroll_rec;
		}
		
		public function scrollTo(point:Point):void{
			scroll_rec.x=point.x;
			scroll_rec.y=point.y;
			canvas.scrollRect=scroll_rec;
			//canvas.x=-point.x;
			//canvas.y=-point.y
		}
		
		public function addPlayer(playerView:Sprite):void{
			
			mapCanvas.addChild(playerView);
		}
		
		public function update():void{
			
			//player.update();
			for each(var unit:CharView in unitDict){
				
				
				if (unit){
					unit.update();
				}
			}
			for each(var spObj:CharView in sprites){
				if (spObj){
					spObj.update();
				}
			}
			camera.update();
			//update_scroll(last_x, last_y)
			//trace(camera.scroll);
			scrollTo(camera.scroll)
			
		}
		
		private var last_x:int,last_y:int;
		public var CENTER_Y:int=100;
		public var CENTER_X:int=100;
		public function update_scroll(last_real_x:Number, last_real_y:Number):void{
			var ax1:int =last_x
			var ay1:int = last_y
			var ax2:int =player.sp.x
			var ay2:int = player.sp.y
			
				var scx:int=ax2-scroll_rec.x
				var scy:int=ay2-scroll_rec.y
			//		trace(scx+":::"+scy)
			if (ax2 < ax1 &&scx<CENTER_X){
				scroll_h(ax1 - ax2)
			}
			if (ax2 > ax1&&scx>CENTER_X ){
				scroll_h(ax1 - ax2)
			}
			
			if (ay2 > ay1 &&scy > CENTER_Y){
				scroll_v(ay1 - ay2)
			}
			if (ay2 < ay1 &&scy < CENTER_Y){
				scroll_v(ay1 - ay2)
			}
			
		}
		
		
		public function scroll(x:Number, y:Number):void{
			
				scroll_h(x)
				scroll_v(y)
			
		}
		
	}
}